package com.example.fitnessservice.module.utils;

import java.util.Calendar;
import java.util.Date;

/**
 * @author feng
 * @version 1.0
 * @&#064;description: TODO
 * @&#064;date 2022/11/22 14:39
 */
public class ConveterUtils {
    public static Integer birthdayToAge(Date date) {
        Calendar now = Calendar.getInstance();
        Calendar birthday = Calendar.getInstance();
        birthday.setTime(date);
        Integer age = now.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
        if (now.get(Calendar.MONTH) < birthday.get(Calendar.MONTH)) {
            age--;
        } else if (now.get(Calendar.MONTH) == birthday.get(Calendar.MONTH) && now.get(Calendar.DAY_OF_MONTH) < birthday.get(Calendar.DAY_OF_MONTH)) {
            age--;
        }
        return age;
    }
}
